using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;

using System.IO;
using System.Xml;

public partial class login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            //GET samlArtifactCookieName
            //CREATE HTTP WEB REQUEST WITH CORDYS URL
            HttpWebRequest samlRequest = (HttpWebRequest)WebRequest.Create(Constants.cordysurl + "/com.eibus.sso.web.authentication.PreLoginInfo.wcp?organization=");
            using (HttpWebResponse samlResponse = (HttpWebResponse)samlRequest.GetResponse())
            {
                if (samlResponse.StatusCode == HttpStatusCode.OK)
                {
                    String xmlresponse = Util.readfromResponse(samlResponse);

                    XmlDocument xmldoc = new XmlDocument();
                    xmldoc.LoadXml(xmlresponse);

                    XmlNodeList nodelist = xmldoc.GetElementsByTagName("AuthConfigMode");
                    Util.authConfigMode = nodelist.Item(0).InnerText;

                    nodelist = xmldoc.GetElementsByTagName("Type");
                    Util.authenticatorType = nodelist.Item(0).InnerText;

                    nodelist = xmldoc.GetElementsByTagName("SamlArtifactCookieName");
                    Util.samlArtifactCookieName = nodelist.Item(0).InnerText;

                    if(Util.authenticatorType.CompareTo("cordys") == 0)
                    {
                        Response.Write("Retrieving User Information");
                    }
                }
            }
        }
        catch (WebException ex)
        {
            if (ex.Status == WebExceptionStatus.ConnectFailure)
            {
                alert("Web Server is not running!! " + ex.Status.ToString());
            }
            else if (ex.Status == WebExceptionStatus.ProtocolError)
            {
                alert("Cordys is not running!! ");
            }
            else if (ex.Status == WebExceptionStatus.NameResolutionFailure)
            {
                alert("Server not Found!! ");
            }
            else
            {
                alert("Connection Failed!! " + ex.Status.ToString());
            }
        }
        catch (Exception ex)
        {
            alert(ex.ToString());
        }
    }
    protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
    {
        Util.insertUserinfointoSAMLRequest(Login1.UserName, Login1.Password);
        
        //GET SAML ARTIFACT
        String responsexml = Util.run(RequestXML.samlrequest);
        
        //ON SUCESSFUL LOGIN SET "TRUE"
        e.Authenticated = Util.tryParseXml(responsexml);

        if (e.Authenticated == true)
        {
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.LoadXml(responsexml);
            Util.assertionArtifact=xmldoc.SelectSingleNode(".//*[local-name()='AssertionArtifact']").InnerText;
        }

        alert(""+Util.tryParseXml(responsexml));
        
    }
    protected void Login1_Init(object sender, EventArgs e)
    {
        
    }

    private void alert(String msg)
    {
        Util.ShowPopUpMsg(msg,this.Page,this.GetType());
    }
}
